﻿using System;
using System.Collections.Generic;
using System.Linq;

using Microsoft.TeamFoundation.WorkItemTracking.Client;


namespace Qetza.TeamFoundation.WorkItemQuery.Admin
{
    /// <summary>
    /// Provides extension methods for the <see cref="StoredQueryCollection"/> class.
    /// </summary>
    public static class StoredQueryCollectionExtensions
    {
        #region Methods

        /// <summary>
        /// Retrieves the <see cref="StoredQuery"/> in the <see cref="StoredQueryCollection"/> with the specified name.
        /// </summary>
        /// <param name="source">
        /// The <see cref="StoredQueryCollection"/> to invoke the find function on.
        /// </param>
        /// <param name="name">
        /// The work item query name.
        /// </param>
        /// <returns>
        /// The <see cref="StoredQuery"/> with the specified name if found; otherwise a null reference.
        /// </returns>
        public static StoredQuery Find(this StoredQueryCollection source, string name)
        {
            // validate parameters
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            // find query
            return source.Cast<StoredQuery>().FirstOrDefault(q => string.Compare(q.Name, name, true) == 0);
        }

        #endregion
    }
}
